//
//  AppDelegate.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "playViewController.h"
#import "SingerState.h"
#import <UIImageView+WebCache.h>
#import "SingerModel.h"
#import "singerActionView.h"
@interface AppDelegate ()<SmailWindowDisappearDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.photoImageview = [[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 50, 50)] autorelease];
    self.songnamelabel = [[[UILabel alloc]initWithFrame:CGRectMake(60, 2, 150, 20)] autorelease];
    self.singerlabel = [[[UILabel alloc]initWithFrame:CGRectMake(60, 25, 100, 20)] autorelease];
    
    self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor =[UIColor whiteColor];
    
    [_window makeKeyAndVisible ];
    
    playViewController *play = [playViewController shareStateOfViewcontroller];
    play.delegate = self;
    MainViewController *view =[[MainViewController alloc]init];
    
    UINavigationController *navi =[[UINavigationController alloc]initWithRootViewController:view];
    
    [self.window setRootViewController:navi];
    
    self.songnamelabel = [[[UILabel alloc]initWithFrame:CGRectMake(60, 2, 200, 20)] autorelease];
    self.songnamelabel.text = @"亲,点首歌吧";
    
    UIImage *image = [UIImage imageNamed:@"placeholder.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.photoImageview.image = image;
    int a = 55;
    self.playview = [[[singerActionView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - a, [UIScreen mainScreen].bounds.size.width, a)] autorelease];
    [self.playview setBackgroundColor:[UIColor whiteColor]];
    [self.playview clickedWithBlock:^(NSInteger temp) {
        [self buttonAction];
    }];
    
    UIButton *xiangbutton = [[[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 10 - a + 10, 10, a - 20, a - 20)] autorelease];
    UIImage *l = [UIImage imageNamed:@"l.png"];
    l = [l imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [xiangbutton setBackgroundImage:l forState:UIControlStateNormal];
    [xiangbutton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.playview addSubview:xiangbutton];
    
    [self.playview addSubview:self.songnamelabel];
    [self.playview addSubview:self.singerlabel];
    [self.playview addSubview:self.photoImageview];
    
//    UIView *photoView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, a, a)] autorelease];
    //暂停键
    self.stopbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.stopbutton setBackgroundColor:[UIColor whiteColor]];
    self.stopbutton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, 10, 35, 35);
    [self.stopbutton addTarget:self action:@selector(stopbuttonaction) forControlEvents:UIControlEventTouchUpInside];
    UIImage *aimage = [UIImage imageNamed:@"iconfont-bofang.png"];
    aimage = [aimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.stopbutton setBackgroundImage:aimage forState:UIControlStateNormal];
    [self.playview addSubview:self.stopbutton];
    //下一个按钮
    UIButton *abutton = [UIButton buttonWithType:UIButtonTypeSystem];
    [abutton setBackgroundColor:[UIColor whiteColor]];
    abutton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 + 60, 15, 30, 30);
    UIImage *nextimage = [UIImage imageNamed:@"next.png"];
    nextimage = [nextimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [abutton setBackgroundImage:nextimage forState:UIControlStateNormal];
    [abutton addTarget:self action:@selector(nsetbutton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.playview addSubview:abutton];
    
    
    [self.window addSubview:self.playview];
    
    
    
    return YES;
}
- (void)nsetbutton
{
    playViewController *play = [playViewController shareStateOfViewcontroller];
    SingerState *singer = [SingerState shareOfPlayState];
    if (singer.playListAddress.count != 0) {
        [play nextButton];
        [self addImageName];
    } else {
        UIAlertView *al = [[[UIAlertView alloc]initWithTitle:@"亲,还没播放哦" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"好的", nil]autorelease];
        [al show];
    }
}
- (void)stopbuttonaction
{
    playViewController *play = [playViewController shareStateOfViewcontroller];
    SingerState *singer = [SingerState shareOfPlayState];
    if (singer.playListAddress.count != 0) {
        [play musicPlay];
        [self addImageName];
        if ([singer.name isEqualToString:@"播放"]) {
            UIImage *image = [UIImage imageNamed:@"iconfont-bofang.png"];
            image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.stopbutton setBackgroundImage:image forState:UIControlStateNormal];
            
        } else {
            UIImage *image = [UIImage imageNamed:@"stop.png"];
            image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.stopbutton setBackgroundImage:image forState:UIControlStateNormal];
        }
        
        
    } else {
        UIAlertView *al = [[[UIAlertView alloc]initWithTitle:@"亲,还没播放哦" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"好的", nil]autorelease];
        [al show];
    }
}

- (void)sendState:(BOOL)state
{
    int a = 55;
    if (state == YES) {
        self.playview.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - a, [UIScreen mainScreen].bounds.size.width, a);
        NSLog(@"abc");
        [self addImageName];
        
        
    } else
    {
        self.playview.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, 0, 0);
        
        NSLog(@"jjj");
    }
}
- (void)buttonAction
{
    playViewController *play = [playViewController shareStateOfViewcontroller];
    play.delegate = self;
    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
    
//    UINavigationController *search1 = [[UINavigationController alloc]initWithRootViewController:play];
    
//    [nav presentViewController:play animated:YES completion:nil];
    [play.view setBackgroundColor:[UIColor whiteColor]];
    [nav presentViewController:play animated:YES completion:nil];
    self.playview.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, 0, 0);
}
- (void)addImageName
{
    SingerState *singer = [SingerState shareOfPlayState];
    SingerModel *model= [singer.playListAddress objectAtIndex:singer.num];
    
    [self.photoImageview removeFromSuperview];
    
    self.photoImageview = [[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 55, 55)] autorelease];
//    NSURL *url = [NSURL URLWithString:singer.pic_url];
    
    UIImage *image = [UIImage imageNamed:@"placeholder.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.photoImageview sd_setImageWithURL:[NSURL URLWithString:singer.pic_url] placeholderImage:image options:SDWebImageRetryFailed];
    [self.playview addSubview:self.photoImageview];
    
    [self.songnamelabel removeFromSuperview];
    self.songnamelabel = [[[UILabel alloc]initWithFrame:CGRectMake(60, 2, 200, 20)] autorelease];
        self.songnamelabel.text = model.name;
    
    [self.playview addSubview:self.songnamelabel];
    
    [self.singerlabel removeFromSuperview];
    self.singerlabel = [[[UILabel alloc]initWithFrame:CGRectMake(60, 25, 100, 15)] autorelease];
    self.singerlabel.font = [UIFont systemFontOfSize:12];
    self.singerlabel.textColor = [UIColor colorWithWhite:0.1 alpha:0.77];
        self.singerlabel.text = model.singerName;
    [self.playview addSubview:self.singerlabel];
    
    
}

//- (void)sendState:(BOOL)state
//{
//    [self.playview setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - a, [UIScreen mainScreen].bounds.size.width, a)];
//}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
